<?php
/********************
 *@param function:頁面管理 model
 *@param datetime 2011-6-7
 *@param author solgh
 ********************/
class Pages_mdl extends CI_model{
  //操作的表.
  const TAB_NAME='clickrcms_page';
  const TAB_DESC='clickrcms_page_description';
  const TAB_IMAGE='clickrcms_page_image';
  function __construct(){
    parent::__construct();
    log_message('debug','Pages_mdl Model class Initialized');
  }

  /****
   *查詢所settings,加分页
   *$data :查询参数.
   */
  public function get_pages($data=array()){
    if(!empty($data)){
      extract($data);
      isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('n.sort_order ASC');
      isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      isset($title) && !empty($title)?$this->db->like('nd.title',$title):'';
      isset($unique_url) && !empty($unique_url)?$this->db->like('n.unique_url',$unique_url):'';
      isset($parent)?$this->db->where('n.parent',$parent):'';
      $this->db->select('n.*,nd.*');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.page_id','LEFT');
      return $this->db->get();
    }
    
  }
  
  /****
   * 統計所有的pages
   */
  public function totle_pages($data=array()){
    if(!empty($data)){
      extract($data);
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      isset($title) && !empty($title)?$this->db->like('nd.title',$title):'';
      isset($unique_url) && !empty($unique_url)?$this->db->like('n.unique_url',$unique_url):'';
      isset($parent)?$this->db->where('n.parent',$parent):'';
      isset($is_new_window) && !empty($is_new_window)?$this->db->where('n.is_new_window',$is_new_window):'';
      $this->db->select('n.*,nd.title');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.page_id','LEFT');
      return $this->db->count_all_results();
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  
  /*****
   * 根據key來刪除
   */
  public function delete_pages($delete_id=''){
    if(!empty($delete_id)){
      // $this->db->where("category_id",$delete_id);
      // $this->db->delete(self::TAB_IMAGE);
      $this->db->where("page_id",$delete_id);
      $this->db->delete(self::TAB_DESC);
      $this->db->where("id",$delete_id);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /****
   *根据ID来查询得到一个信息.
   *$id :要查詢的ID
   */
  public function get_pages_by_id($id=''){
    if(!empty($id)){
      $data=array();
      $this->db->where('id',$id);
      $get_result=$this->db->get(self::TAB_NAME);
      if($get_result->num_rows()>0)
      {
        $data=$get_result->row_array();
      }
      $get_result->free_result();
      return $data;
    }
  }
  /***
   * 修改一個pages
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_pages($data=array(),$b_id=0){
    if(!empty($data) && !empty($b_id)){
      extract($data);
      $this->db->where('id',$b_id);
      $this->db->update(self::TAB_NAME,$general);
      //描述.
      if(isset($descriptions) && is_array($descriptions)){
        $this->db->where('page_id',$b_id);
        $this->db->delete(self::TAB_DESC);
        foreach ($descriptions as $language_id => $value) {
          $value['page_id']=$b_id;
          $value['language_id']=$language_id;
          $this->db->insert(self::TAB_DESC,$value);
        } //end foreach;
      }
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 新增一個 pages
   */
  public function add_pages($data=array()){
    if(!empty($data)){
      extract($data);
      $this->db->insert(self::TAB_NAME,$general);
      $insert_id=$this->db->insert_id(); //獲取新增id
      if($insert_id>0){
        //描述.
        if(isset($descriptions) && is_array($descriptions)){
          foreach ($descriptions as $language_id => $value) {
            $value['page_id']=$insert_id;
            $value['language_id']=$language_id;
            $this->db->insert(self::TAB_DESC,$value);
          } //end foreach;
        }
      }//end if;
      return ($this->db->affected_rows()>0)?$insert_id:false;
    }
  }
  
  //根據id 獲取 多語言的descriptions
  public function get_descriptions($id=0){
    if(!empty($id)){
        $this->db->where('page_id',$id);
        return $this->db->get(self::TAB_DESC);
    }
  }

  //--Banner廣告--//
  //根據id 獲取 多圖片
  public function get_images($data=array()){
    if(!empty($data)){
        extract($data);
        $this->db->order_by('sort_order ASC');
        $this->db->where('category_id',$album_id);
        isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
        return $this->db->get(self::TAB_IMAGE);
    }
  }
  
    //根據id 獲取 多圖片
  public function totle_images($data=array()){
    if(!empty($data)){
      extract($data); 
      $this->db->order_by('sort_order ASC');
      $this->db->where('category_id',$album_id);
      return $this->db->count_all_results(self::TAB_IMAGE);
    }else{
      return $this->db->count_all(self::TAB_IMAGE);
    }
  }
  //添加相簿图片
  public function add_images($data=array()){
    if(!empty($data)){
        $this->db->insert(self::TAB_IMAGE,$data);
        return ($this->db->affected_rows()>0)?true:false;
    }
  }
  //修改相簿圖片
  public function edit_images($data=array(),$image_id=0){
    if(!empty($data) && !empty($image_id)){
      $this->db->where('id',$image_id);
      $this->db->update(self::TAB_IMAGE,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  //刪除相簿圖片
  public function delete_image($id=0){
    if(!empty($id)){
        $this->db->where('id',$id);
        $this->db->delete(self::TAB_IMAGE);
        return ($this->db->affected_rows()>0)?true:false;
    }
  }

  
  //(前臺) 更新瀏覽次數
  public function add_view_num($id=''){
    if(!empty($id)){
      $this->db->where('id',$id);
      $this->db->set('view_num', 'view_num+1', FALSE);
      $this->db->update(self::TAB_NAME);
    }
  }
  //(前臺) 根據unique_url查詢.
  public function get_pages_by_unique_url($data=array()){
    if(!empty($data)){
      extract($data);
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($language_id) && !empty($language_id)?$this->db->where('nd.language_id',$language_id):'';
      isset($id) && !empty($id)?$this->db->where('n.id',$id):'';
      isset($unique_url) && !empty($unique_url)?$this->db->where('n.unique_url',$unique_url):'';
      $this->db->select('n.*,nd.*');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_DESC.' AS nd', 'n.id = nd.page_id','LEFT');
      $query=$this->db->get();
      $return=array();
      if($query->num_rows()>0)
      {
        $return=$query->row_array();
      }
      $query->free_result();
      return $return;
    }
  }
}
